package com.vercon.moensmart.dialog.base

import android.app.Activity
import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.util.DisplayMetrics
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.InputMethodManager
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentManager
import androidx.viewbinding.ViewBinding
import com.vercon.common.R
import me.jessyan.autosize.AutoSize

abstract class BaseDialogFragment<T : ViewBinding?> : DialogFragment() {

    protected val TAG = this.javaClass.simpleName
    var binding: T? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(STYLE_NORMAL, R.style.dialog_soft_input)
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        AutoSize.autoConvertDensityOfGlobal(context as Activity)
        binding = onCreateViewBinding(inflater, container)
        return binding?.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        //初始化界面

        //初始化
        initView(view)
        //初始化监听
        initListener(view)
        hideBottomMenu()
    }

    protected abstract fun onCreateViewBinding(inflater: LayoutInflater, parent: ViewGroup?): T
    protected abstract fun initView(view: View?)
    protected abstract fun initListener(view: View?)
    protected abstract val isFullScreen: Boolean
    override fun onStart() {
        super.onStart()
        val dm = DisplayMetrics()
        activity?.windowManager?.defaultDisplay?.getMetrics(dm)
        if (isFullScreen) {
            dialog!!.window!!
                .setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
            dialog!!.window!!.setBackgroundDrawable(ColorDrawable(Color.BLACK))
        } else {
            dialog!!.window!!
                .setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
            dialog!!.window!!.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        hideBottomMenu()
    }

    //全局弹框提示用此方法
    fun showAllowingStateLoss(manager: FragmentManager, tag: String?) {
        try {
            val dismissed = DialogFragment::class.java.getDeclaredField("mDismissed")
            dismissed.isAccessible = true
            dismissed[this] = false
        } catch (e: NoSuchFieldException) {
            e.printStackTrace()
        } catch (e: IllegalAccessException) {
            e.printStackTrace()
        }
        try {
            val shown = DialogFragment::class.java.getDeclaredField("mShownByMe")
            shown.isAccessible = true
            shown[this] = true
        } catch (e: NoSuchFieldException) {
            e.printStackTrace()
        } catch (e: IllegalAccessException) {
            e.printStackTrace()
        }
        val ft = manager.beginTransaction()
        ft.add(this, tag)
        ft.commitAllowingStateLoss()
    }

    fun hideBottomMenu() {
        //隐藏虚拟按键，并且全屏
        //for new api versions.
        val decorView = activity?.window?.decorView
        val uiOptions = (View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                or View.SYSTEM_UI_FLAG_FULLSCREEN)
        decorView?.systemUiVisibility = uiOptions
    }

    fun hideKeyboard(token: View?) {
        if (token != null) {
            val im = activity?.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
            im.hideSoftInputFromWindow(token.windowToken, InputMethodManager.HIDE_NOT_ALWAYS)
        }
    }

    fun showKeyboard(view: View?) {
        //显示键盘
        if (view != null && context != null) {
            val im = context?.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
            if (im != null) {
                view.requestFocus()
                im.showSoftInput(view, 0)
            }
        }
    }
}